<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        isNeedSelection
        ref="table"
        :btnList="btnList"
        :total="totalNum"
        v-model="searchParams"
        :searchShowList="searchShowList"
        :searchHideList="searchHideList"
        :tableColumn="tableColumn"
        :minSearchLabelWidth="128"
        requestName="searchHandle"
        :tableData="tableData"
        @add="operateHandle($event, 'add')"
        @remove="operateHandle($event, 'remove')"
        @export="operateHandle($event, 'export')"
        @edit="operateHandle($event, 'edit')"
        @view="operateHandle($event, 'view')"
      />
    </div>

    <TrEditPopup
      ref="TrEditPopup"
      :width="460"
      v-model="isShowEdit"
      :title="checkTitle"
      :labelWidth="128"
      :valueObj.sync="formObj"
      :formData="formData"
      :list="popupList"
      @close="isShowEdit = false"
      @confrim="editConfirmHandle"
      @select-change="selectChangeHandle"
    />

    <TrDetail
      v-model="isShowDetailPopup"
      popupTitle="仓库维护"
      :leftList="detailList"
      statusModel="hidden"
      :labelWidth="112"
      :detailData="detailData"
      :isNeedFooter="false"
      @close="closeDetailPopup"
    />
  </div>
</template>

<script>
import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrEditPopup from "@/components/Tr-edit/Tr-edit-popup.vue";
import TrDetail from "@/components/Tr-detail/Tr-detail.vue";

import enumData from "@/utils/enumCode.js";

import {
  getList,
  add,
  remove,
  update,
} from "@/api/baseCode/warehouseBussinessType";
export default {
  components: {
    TrTable,
    TrEditPopup,
    TrDetail,
  },
  name: "warehouseMaintenance",
  data: () => ({
    tableColumn: [
      {
        type: "text",
        label: "仓库业务类型代码",
        value: "code",
      },
      {
        type: "text",
        label: "仓库业务类型前缀",
        value: "prefix",
      },
      {
        type: "text",
        label: "仓库业务类型名称",
        value: "name",
      },
      {
        type: "text",
        label: "仓库业务交易方向",
        value: "transDirectionName",
      },
      {
        type: "text",
        label: "转出库存类型",
        value: "inventoryTurnOutName",
      },
      {
        type: "text",
        label: "转入库存类型",
        value: "inventoryTurnInName",
      },
      {
        type: "text",
        label: "创建人员",
        value: "createUserName",
      },
      {
        type: "text",
        label: "创建时间",
        value: "createTime",
      },
      {
        type: "text",
        label: "修改人员",
        value: "updateUserName",
      },
      {
        type: "text",
        label: "修改时间",
        value: "updateTime",
      },
      {
        type: "option",
        label: "操作",
        fixed: "right",
        width: 120,
        showBtnList: [
          {
            label: "查看",
            option: "view",
          },
          {
            label: "编辑",
            option: "edit",
          },
        ],
      },
    ],
    searchHideList: [
      {
        id: "prefix",
        type: "input",
        width: 33.33,
        label: "仓库业务类型前缀",
        value: "",
      },
      {
        id: "name",
        type: "input",
        width: 33.33,
        label: "仓库业务类型名称",
        value: "",
      },
      {
        id: "transDirectionCode",
        type: "select",
        width: 33.33,
        label: "仓库业务交易方向",
        value: "",
        list: [],
      },
      {
        id: "code",
        type: "select",
        width: 33.33,
        label: "仓库业务类型代码",
        value: "",
        list: [],
      },
      {
        id: "",
        type: "",
        width: 33.33,
        label: "",
        value: "",
        list: [],
      },

      {
        startStr: "updateTimeStart",
        endStr: "updateTimeEnd",
        id: "modifyTime",
        type: "daterange",
        width: 80,
        label: "修改时间",
        value: [],
        timePeriod: true,
        radio: -1,
      },
    ],
    searchShowList: [
      {
        isNeed: true,
        startStr: "createTimeStart",
        endStr: "createTimeEnd",
        id: "createdTime",
        type: "daterange",
        width: 80,
        label: "创建时间",
        value: [],
        timePeriod: true,
        radio: 1,
      },
    ],
    btnList: [
      {
        label: "新增",
        option: "add",
        color: "blue",
      },
      {
        label: "删除",
        option: "remove",
        color: "white",
        isRelaTable: true,
      },
      // {
      //   label: "导出",
      //   option: "export",
      //   color: "white",
      // },
    ],
    tableData: [],
    searchParams: {
      current: 1,
      size: 10,
    },
    totalNum: 0,

    detailData: {},
    isShowDetailPopup: false,
    detailList: [
      {
        type: "text",
        label: "基础信息",
        list: [
          {
            text: "仓库业务类型代码",
            id: "code",
            width: 50,
          },
          {
            text: "仓库业务类型前缀",
            id: "prefix",
            width: 50,
          },
          {
            text: "仓库业务交易方向",
            id: "transDirectionName",
            width: 50,
          },
          {
            text: "仓库业务类型名称",
            id: "name",
            width: 50,
          },
          {
            text: "转入库存地类型",
            id: "inventoryTurnInName",
            width: 50,
          },
          {
            text: "转出库存地类型",
            id: "inventoryTurnOutName",
            width: 50,
          },
        ],
      },
    ],

    // 新增编辑弹窗
    formObj: {},
    formData: {},
    popupList: [
      {
        label: "仓库业务类型代码",
        type: "select",
        width: 100,
        value: "",
        id: "code",
        list: [],
        labelWord: "value",
        valueWord: "value",
      },
      {
        label: "仓库业务类型前缀",
        type: "input",
        width: 100,
        value: "",
        id: "prefix",
      },
      {
        label: "仓库业务类型名称",
        type: "select",
        width: 100,
        value: "",
        id: "name",
        list: [],
        labelWord: "label",
        valueWord: "label",
      },
      {
        label: "仓库业务类型方向",
        type: "select",
        width: 100,
        value: "",
        id: "transDirectionCode",
        list: [],
      },
      {
        label: "转出库存地类型",
        type: "select",
        width: 100,
        value: "",
        id: "inventoryTurnOutType",
        list: [],
      },
      {
        label: "转入库存地类型",
        type: "select",
        width: 100,
        value: "",
        id: "inventoryTurnInType",
        list: [],
      },
    ],
    isShowEdit: false,
    currentHandle: "",
    currentRowVal: {},
  }),
  computed: {
    checkTitle() {
      const titles = {
        add: "新增",
        default: "编辑",
      };
      return titles[this.currentHandle] || titles.default;
    },
    ids() {
      let ids = [];
      this.$refs.table.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
  },
  async mounted() {
    await this.getEnmuData();
    // this.getListHandle();
  },
  methods: {
    async getEnmuData() {
      const [
        warehouseDirection,
        warehouseTurnOut,
        warehouseTurnIn,
        warehouseBusinessType,
      ] = await Promise.all([
        this.$fetchEnumData([enumData.warehouseDirection]),
        this.$fetchEnumData([enumData.warehouseTurnOut]),
        this.$fetchEnumData([enumData.warehouseTurnIn]),
        this.$fetchEnumData([enumData.warehouseBusinessType]),
      ]);

      this.updateEnumData(
        warehouseDirection["PC-WAREHOUSE-DIRECTION"],
        warehouseTurnOut["PC-WAREHOUSE-TURN-OUT"],
        warehouseTurnIn["PC-WAREHOUSE-TURN-IN"],
        warehouseBusinessType["PC-WAREHOUSE-BUSINESS-TYPE"]
      );
    },

    updateEnumData(
      warehouseDirection,
      warehouseTurnOut,
      warehouseTurnIn,
      warehouseBusinessType
    ) {
      this.$set(
        this.$findByProperty(this.popupList, "id", "transDirectionCode"),
        "list",
        warehouseDirection
      );
      this.$set(
        this.$findByProperty(this.searchHideList, "id", "transDirectionCode"),
        "list",
        warehouseDirection
      );
      this.$set(
        this.$findByProperty(this.popupList, "id", "inventoryTurnOutType"),
        "list",
        warehouseTurnOut
      );
      this.$set(
        this.$findByProperty(this.popupList, "id", "inventoryTurnInType"),
        "list",
        warehouseTurnIn
      );
      this.$set(
        this.$findByProperty(this.searchHideList, "id", "code"),
        "list",
        warehouseBusinessType.map((item) => ({
          label: item.value,
          value: item.value,
        }))
      );
      this.$set(
        this.$findByProperty(this.popupList, "id", "code"),
        "list",
        warehouseBusinessType
      );
      this.$set(
        this.$findByProperty(this.popupList, "id", "name"),
        "list",
        warehouseBusinessType
      );
    },

    async operateHandle(val, type) {
      this.currentHandle = type;

      const codeItem = this.$findByProperty(this.popupList, "id", "code");

      codeItem.disabled = type == "edit";

      const nameItem = this.$findByProperty(this.popupList, "id", "name");

      nameItem.disabled = type == "edit";

      this.formData = {};

      const actions = {
        add: () => (this.isShowEdit = true),
        edit: () => {
          this.currentRowVal = val;

          this.formData = val;

          this.isShowEdit = true;
        },
        remove: () => this.removeHandle(),
        export: () =>
          this.$exportHandle({ vm: this, params: this.searchParams }),
        view: () => this.getDetailHandle(val),
      };

      const action = actions[type];
      if (action) action();
    },

    selectChangeHandle(event) {
      const cloneList = this.$refs.TrEditPopup?.$refs.trEdit?.cloneList;
      if (event.id === "code") {
        const item = this.$findByProperty(cloneList, "id", "name");

        const name = item.list.find((item) => item.value === event.value);

        this.$set(item, "value", name.label);
      } else if (event.id === "name") {
        const item = this.$findByProperty(cloneList, "id", "code");

        const code = item.list.find((item) => item.label === event.value);

        this.$set(item, "value", code.value);
      }
    },

    getDetailHandle(val) {
      this.detailData = val;

      this.isShowDetailPopup = true;
    },
    closeDetailPopup() {
      this.isShowDetailPopup = false;
    },

    searchHandle() {
      this.getListHandle();
    },
    async removeHandle() {
      const result = await this.$textShow({
        title: this.$t("tip.label"),
        content: [this.$t("tip.delete")],
      });
      if (!result) return;

      const { data } = await remove(this.ids);
      if (data.code !== 200) return;

      this.$message.success(this.$t("msg.success"));
      this.searchParams.current = 1;
      this.getListHandle();
    },
    async getListHandle() {
      const { data } = await getList(this.searchParams);
      if (data.code != 200) return;
      this.$validateData(data.data);
      this.tableData = data.data.records;
      this.totalNum = Number(data.data.total);
    },
    async editHandle() {
      const { data } = await update({
        ...this.formObj,
        id: this.currentRowVal.id,
      });
      return data.code === 200;
    },
    async addHandle() {
      const { data } = await add(this.formObj);
      return data.code === 200;
    },
    async editConfirmHandle() {
      let flag = false;

      flag =
        this.currentHandle === "edit"
          ? await this.editHandle()
          : await this.addHandle();

      if (!flag) return;
      this.$message.success(this.$t("msg.success"));
      this.isShowEdit = false;
      this.searchParams.current = 1;
      this.getListHandle();
    },
  },
};
</script>
